package com.weixing.mall.pay.channel.ali.payment;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.weixing.mall.pay.PayParam;
import com.weixing.mall.pay.PayResult;
import com.weixing.mall.pay.channel.ali.AbstractAliPay;
import com.weixing.mall.pay.channel.ali.AliPaySet;
import com.weixing.mall.pay.channel.ali.AliTradeTypeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName AliPayPc
 * @Description 手机网站支付
 * @Author Small
 * @Date 2019/11/27 18:56
 * @Version 1.0
 **/
@Slf4j
public class AliPayWap extends AbstractAliPay {


    private AlipayTradeWapPayRequest alipayRequest;
    private String PRODUCT_CODE = "QUICK_WAP_WAY";

    public AliPayWap(AliPaySet aliPaySet) {
        super(aliPaySet);
    }

    @Override
    public void initAliPayRequest() {
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setReturnUrl(aliPaySet.getReturnUrl());
        alipayRequest.setNotifyUrl(aliPaySet.getNotifyUrl());
        this.alipayRequest = alipayRequest;
    }

    @Override
    public void setPayRequestParam(PayParam payParam) {
        this.alipayRequest.setBizContent(JSON.toJSONString(wrapperRequestData(payParam, PRODUCT_CODE)));
    }


    @Override
    public PayResult executePay() throws AlipayApiException {
        AlipayTradeWapPayResponse payResponse = this.alipayClient.pageExecute(alipayRequest);
        String body = payResponse.getBody();
        if (payResponse.isSuccess()) {
            return PayResult.success(body);
        } else {
            String subCode = payResponse.getSubCode();
            String subMsg = payResponse.getSubMsg();
            return PayResult.fail(subCode, subMsg);
        }
    }

    @Override
    public AliTradeTypeEnum getTradeType() {
        return AliTradeTypeEnum.WAP;
    }
}
